﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Converter;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.Resources;
using System.Reflection;
using Accounting.Library;

namespace csla4Skeleton.UI.SL.NghiepVu.THUE.THUE_BangKeBanRa
{
    public partial class FormThongTin : ChildWindow
    {
        THUE_BangKeBanRa_ListViewModel ListViewModel;
        THUE_BangKeBanRa_ChildWindow thue_BangKeBanRa_ChildWindow;
        public event EventHandler Saved;
        public FormThongTin(THUE_BangKeBanRa_ListViewModel listViewModel)
        {        
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("ccsla4Skeleton.UI.SL.NghiepVu.THUE.THUE_BangKeBanRa.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.ListViewModel = listViewModel;
           
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void TinhBangKeBanRaButton_Click(object sender, RoutedEventArgs e)
        {

            if (ThangTextBox.Text.Equals("") || ThangTextBox.Text == null || NamTextBox.Text.Equals("") || NamTextBox.Text == null)
            {
                MessageBoxResult notice = MessageBox.Show("Bạn chưa nhập liệu đầy đủ", "Chưa nhập liệu", MessageBoxButton.OKCancel);
            }
            else
            {
                IList<THUE_BangKeBanRaInfo> list = ListViewModel.Model.getListOfInfo();
                THUE_BangKeBanRaInfo BangKeBanRa = null;
                foreach (THUE_BangKeBanRaInfo item in list)
                {
                    if (item.Thang.ToString().Equals(ThangTextBox.Text) && item.Nam.ToString().Equals(NamTextBox.Text))
                    {
                        BangKeBanRa = item;
                    }
                }
                if (BangKeBanRa != null)
                {
                   
                    MessageBoxResult result = MessageBox.Show("Chứng từ khấu hao này đã tồn tại, bạn có muốn xem hay không?", "Xem chứng từ khấu hao", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {

                        thue_BangKeBanRa_ChildWindow = new THUE_BangKeBanRa_ChildWindow(BangKeBanRa.Khoa);
                        thue_BangKeBanRa_ChildWindow.Show();
                        this.Close();
                    }
                     
                }
                else
                {
                
                    thue_BangKeBanRa_ChildWindow = new THUE_BangKeBanRa_ChildWindow(this.ThangTextBox.Text, this.NamTextBox.Text);
                    thue_BangKeBanRa_ChildWindow.Show();
                    thue_BangKeBanRa_ChildWindow.Visibility = Visibility.Collapsed;
                   
                    this.TinhBangKeBanRaItemButton.IsEnabled = false;
                    this.BoQuaItemButton.IsEnabled = false;
                    thue_BangKeBanRa_ChildWindow.TinhBangKeBanRaCompleteted += new EventHandler(OnTinhBangKeBanRaCompleteted);
                    thue_BangKeBanRa_ChildWindow.OnClosedChildWindow += new EventHandler(OnClosedWindow);
                }
            }
            
        }
       

        private void OnClosedWindow(object sender, EventArgs e)
        {
            this.Close();
        }
        private void OnTinhBangKeBanRaCompleteted(object sender, EventArgs e)
        {
            if (thue_BangKeBanRa_ChildWindow.countSave == 0)
            {
                MessageBoxResult result = MessageBox.Show("Bạn có muốn xem chi tiết bảng kê?", "Xem chi tiết", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    thue_BangKeBanRa_ChildWindow.Visibility = Visibility.Visible;
                    if (Saved != null)
                    {
                        Saved(sender, e);
                    }
                    thue_BangKeBanRa_ChildWindow.countSave++;
                    //this.Close();
                }
                else
                {
                    thue_BangKeBanRa_ChildWindow.Close();
                    if (Saved != null)
                    {
                        Saved(sender, e);
                    }
                    //this.Close();
                }
                
            }
            else
            {
                
                if (Saved != null)
                {
                    Saved(sender, e);
                }
                
            }
           
        }

        
    }
}

